<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增课程</title>

    <link rel="stylesheet" href="__STATIC__/admin/js/layer/theme/default/layer.css">
    <link rel="stylesheet" href="__STATIC__/admin/js/layui/css/layui.css">
    <link rel="stylesheet" href="__STATIC__/admin/js/jqzoom/jqzoom.css">
    <link rel="stylesheet" href="__STATIC__/admin/css/coursesCommon.css">
    <link href="__STATIC__/admin/js/bootstrap/css/bootstrap.css"
          rel="stylesheet">
    <link href="__STATIC__/admin/css/AdminLTE.css" rel="stylesheet" type="text/css" />


</head>
<body class="wst-page">
<form method="post"  enctype="multipart/form-data" id="upload_form" >
<table class="table table-hover">
    <tr>
        <th align='right'>课程名字</th>
        <td><input type="text" name="courses_name" value="" class="courses_name"></td>
    </tr>
    <tr>
        <th align='right'>课程标题</th>
        <td><input type="text" name="courses_title" value="" class="courses_title"></td>
    </tr>
    <tr>
        <th align='right'>课程内容(视频上传)</th>
        <td><input type="file" name="video_Upload" value="" onchange="video()"></td>
        <input type="hidden" id="courses_content" name="courses_content" value="" class="courses_content">
    </tr>
    <tr>
        <th align='right'>封面图片</th>
        <td><input type="file" name="photo_Upload" value="" onchange="fileImg()" >
            <img id="iMgc" src="__STATIC__/admin/img/fm.jpg" alt="封面" width="200px" height="150px">
        </td>
        <input type="hidden" id="courses_photo" name="courses_photo" value="" class="courses_photo">
    </tr>
    <tr>
        <th align='right'>课程分类</th>
        <td>
            <select class="classify_id">
                {volist name="res" id="v"}
                <option value="{$v.classify_id}">{$v.course_classify}</option>
                {/volist}
            </select>
        </td>
    </tr>
    <tr>
        <th align='right'>课程金额</th>
        <td><input type="text" name="courses_money" value="" class="courses_money"></td>
    </tr>
    <tr>
        <td><span class="span layui-btn layui-btn-normal">新增</span></td>
        <td><span class="span_1 layui-btn layui-btn-normal">返回</span></td>
    </tr>
</table>
</form>

</body>

<script src="__STATIC__/admin/js/jquery.min.js"></script>
<script src="__STATIC__/admin/js/layer/layer.js"></script>
<script src="__STATIC__/admin/js/layui/layui.js"></script>
<script src="__STATIC__/admin/js/jqzoom/jqzoom.js"></script>

<script>

    //提交数据jquery
    $(function() {
        $('.span').click(function () {

            var data = {};
            data.courses_name = $(".courses_name").val();
            data.courses_title = $(".courses_title").val();
            data.courses_content = $(".courses_content").val();
            data.classify_id = $(".classify_id").val();
            data.courses_photo = $(".courses_photo").val();
            data.courses_money = $(".courses_money").val();

            if( data.courses_name == ""){
                layer.msg('课程名不能为空', {icon: 2});
                return;
            }
            if( data.courses_title == ""){
                layer.msg('课程标题不能为空', {icon: 2});
                return;
            }
            if( data.courses_money == ""){
                layer.msg('课程金额不能为空', {icon: 2});
                return;
            }

            layer.load();
            setTimeout(function(){
                layer.closeAll('loading');
            }, 2000);

            $.post("{:url('admin/Courses/cuorsesInsert')}",data,function (data) {

                if(data > 0){
                    layer.msg('新增成功', {icon: 1,time: 2000});

                    window.location.href = ("{:url('admin/courses/index')}");
                }else {
                    layer.msg('新增失败', {icon: 2});

                }
            })

        })
    });



    //放大镜jquery
    $(function() {
        $("#divImg").jqueryzoom({
            xzoom: 800,
            yzoom: 800,
            offset: 10,
            position: "right",
            preload: 1
        });
    });


    //上传图片jquery
    function fileImg() {

        var upload_form = $("#upload_form")[0];
        var data_obj = new FormData(upload_form);

        layer.load();
        setTimeout(function(){
            layer.closeAll('loading');
        }, 5000);

        $.ajax({
            url: "{:url('admin/Courses/uploadImg')}",
            method: "POST",
            data: data_obj,
            contentType: false,//文件上传必须
            processData: false,//文件上传必须
            success: function (data) {
                var jost = JSON.parse( data );

                    if(jost != 1){
                        layer.msg('上传成功', {icon: 1,time: 2000});
                        var imgUrl = "__UPLOADS__/photo/"+jost;
                        $("#iMgc").attr('src',imgUrl);
                        $("#courses_photo").attr('value',imgUrl);
                    }else{
                        layer.msg('上传失败', {icon: 2,time: 2000});

                    }
            }
        });
    }

    //视频上传
    function video() {
        var upload_form = $("#upload_form")[0];
        var data_obj = new FormData(upload_form);

        layer.load();
        setTimeout(function(){
            layer.closeAll('loading');
        }, 5000);

        $.ajax({
            url: "{:url('admin/Courses/video')}",
            method: "POST",
            data: data_obj,
            contentType: false,//文件上传必须
            processData: false,//文件上传必须
            success: function (data) {
                var jost = JSON.parse( data );

                if(jost != 1){

                    layer.msg('上传成功', {icon: 1,time: 2000});

                    var imgUrl = "__UPLOADS__/video/"+jost;

                    $("#courses_content").attr('value',imgUrl);

                }else{
                    layer.msg('上传失败', {icon: 2,time: 2000});

                }
            }
        });
    }



    //跳回首页jquery
    $(function() {
        $('.span_1').click(function () {

                layer.load();
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 2000);

                window.location.href = ("{:url('admin/courses/index')}");

        })
    });

</script>

</html>